package com.cty.BruteForceMatching;

public class BruteForceMatching {
    public static void main(String[] args) {
        String str1 = "硅硅谷 尚硅谷你尚硅 尚硅谷你尚硅谷你尚硅你好";
        String str2 = "尚硅谷你尚硅你~";
        int index = BruteForceMatching(str1,str2);
        System.out.println("找到的索引" + index);
    }
    public static int BruteForceMatching(String str1,String str2) {
        char[] s1 = str1.toCharArray();
        char[] s2 = str2.toCharArray();
        int i = 0;
        int j = 0;
        int str1Len = s1.length;
        int str2Len = s2.length;
        while (i < str1Len && j < str2Len) {
            if (s1[i] == s2[j]) {
                i++;
                j++;
            } else {
                i = i - j + 1;
                j = 0;
            }
        }
        if (j == str2Len) {
            return i - j;
        } else {
            return -1;
        }
    }
}
